package com.platform.util;

import java.util.HashMap;
import java.util.Map;

import com.platform.service.SysConfigService;
import com.platform.utils.SpringContextUtils;

public class SysConfigUtil {
    private static SysConfigService sysConfigService;

    /**
     * 系统参数
     */
    private static Map<String,String> sysConfig;
    
    /**
     * 自定义参数
     */
    private static Map<String,Map<String,String>> configMap;
    
    static {
    	SysConfigUtil.sysConfigService = (SysConfigService) SpringContextUtils.getBean("sysConfigService");
    	sysConfig = new HashMap<String,String>();
    	configMap = new HashMap<String,Map<String,String>>();
    }
    
    public static Map<String,String> getConfigsByType(String type,boolean forceRefresh){
    	if(configMap.get(type) == null || forceRefresh) {
    		configMap.put(type,sysConfigService.getMap(type));
    	}
		return configMap.get(type);
    }
    
    public static String getConfigsByTypeKey(String type,String key,boolean forceRefresh){
    	if(configMap.get(type) == null || configMap.get(type).get(key) == null || forceRefresh) {
    		configMap.put(type,sysConfigService.getMap(type));
    	}
		return configMap.get(type).get(key);
    }    
    
    public static String getSysConfig(String key) {
		return getSysConfig(key,null,false);
    }

    public static String getSysConfig(String key,String defaultValue) {
		return getSysConfig(key,defaultValue,false);
    }
    
    public static String getSysConfig(String key,String defaultValue,boolean forceRefresh) {
    	if(sysConfig.get(key) == null || forceRefresh) {
    		sysConfig.put(key,sysConfigService.getValue(key, defaultValue));
    		return sysConfig.get(key);
    	}
		return sysConfig.get(key);
    }
    
    public static void putSystemConfig(String key,String value) {
		sysConfig.put(key,value);
    }
    
    public static void clear() {
    	sysConfig.clear();
    	configMap.clear();
    }
    ///////////////////////////////////////////////////////////////////////////////////////////
}
